﻿using System;
using System.Collections.Generic;
#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif
using Kugar.MVC.MobileUI.WeUI.Helpers;

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageForm
{
    public class PageForm_TextBoxWithSelector : PageForm_FieldBase
    {
        private string _text = "";
        private string _placeHolder = "";
        private InputBoxType _type = InputBoxType.text;
        private string _pattern = "";
        private List<SelectorItem> _lst = new List<SelectorItem>(5);
        private string _selectorValue = "";
        private string _selectorName = "";
        private string _textname = "";
        private string _textvalue = "";

        public PageForm_TextBoxWithSelector(ViewContext viewContext) : base(viewContext)
        {
            _attrs.Class("weui-cell weui-cell_select weui-cell_select-before");
        }

        public PageForm_TextBoxWithSelector FirstItem(string title, string value)
        {
            _lst.Insert(0, new SelectorItem(title, value));

            return this;
        }

        public PageForm_TextBoxWithSelector Options<T>(IEnumerable<T> source, Func<T, string> titleFunc,
            Func<T, string> valueFunc)
        {
            _lst.Clear();

            _lst.AddRange(OptionHelper.Convert(source, titleFunc, valueFunc));

            return this;
        }

        public PageForm_TextBoxWithSelector Option(Type enumType)
        {
            _lst.Clear();

            _lst.AddRange(OptionHelper.Convert(enumType));

            return this;
        }

        public PageForm_TextBoxWithSelector Text(string text)
        {
            _text = text;

            return this;
        }

        public PageForm_TextBoxWithSelector SelectorValue(string value)
        {
            _selectorValue = value;

            return this;
        }

        public PageForm_TextBoxWithSelector SelectorName(string name)
        {
            _selectorName = name;

            return this;
        }

        public PageForm_TextBoxWithSelector TextboxName(string name)
        {
            _textname = name;

            return this;
        }

        public PageForm_TextBoxWithSelector TextboxValue(string value)
        {
            _textvalue = value;

            return this;
        }

        public PageForm_TextBoxWithSelector Placeholder(string text)
        {
            _placeHolder = text;

            return this;
        }

        public PageForm_TextBoxWithSelector Type(InputBoxType type)
        {
            _type = type;

            return this;
        }

        public PageForm_TextBoxWithSelector Pattern(string pattern)
        {
            _pattern = pattern;

            return this;
        }

        public override MvcHtmlString Render()
        {
            ViewContext.Writer.WriteLine($"<div class=\"weui-cells__title\">{_label}</div>");

            if (Html.ViewData.ModelState.ContainsKey(_textname) || Html.ViewData.ModelState.ContainsKey(_selectorName))
            {
                _attrs.Class("weui-cell_warn");
            }
            
            ViewContext.Writer.WriteLine($"<div {_attrs}>");

            ViewContext.Writer.WriteLine("<div class=\"weui-cell__hd\">");

            ViewContext.Writer.WriteLine($"<select class=\"weui-select\" name=\"{_selectorName}\">");

            foreach (var item in _lst)
            {
                ViewContext.Writer.WriteLine($"<option {(string.IsNullOrWhiteSpace(_selectorValue)?"": "selected=\"selected\"")}  value=\"{item.Value}\">{item.Title}</option>");
            }

            ViewContext.Writer.WriteLine("</select>");

            ViewContext.Writer.WriteLine("</div> <!-- End  cell__hd-->");

            ViewContext.Writer.WriteLine("<div class=\"weui-cell__bd\">");

            ViewContext.Writer.WriteLine($"<input class=\"weui-input\" type=\"{_type}\" pattern=\"{_pattern}\" placeholder=\"{_placeHolder}\" name=\"{_textname}\" value=\"{_textvalue}\" />");

            ViewContext.Writer.WriteLine("</div> <!-- End cell__bd -->");

            ViewContext.Writer.WriteLine("<div class=\"weui-cell__ft\">");

            ViewContext.Writer.WriteLine("<i class=\"weui-icon-warn\"></i>");

            ViewContext.Writer.WriteLine("</div>");

            ViewContext.Writer.WriteLine("</div> <!-- End Field -->");

            return MvcHtmlString.Empty;

        }
    }
}